#include "dictionarydialog.h"
#include "textdialog.h"
#include "textkvdialog.h"
#include "ui_dictionarydialog.h"

#include <QMessageBox>


DictEntry::DictEntry(const QString &index, const QString &original, const QString &meaning, bool enabled)
    : index(index), original(original), meaning(meaning), enabled(enabled)
{

}


DictionaryDialog::DictionaryDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DictionaryDialog)
    , modified(false) // 初始化 modified 为 false
{
    ui->setupUi(this);

    // 初始化 QSettings，指定组织名称和应用名称
    settings = new QSettings("LogAnalyzer", "dict", this);

    // 加载词典配置
    loadDictionary();

    // 连接信号槽，确保在窗口关闭时保存词典配置
    connect(this, &DictionaryDialog::finished, this, [this]() {
        if (modified) {
            saveDictionary();
        }
    });

    // 连接表格内容修改信号
    connect(ui->tableWidget, &QTableWidget::itemChanged, this, &DictionaryDialog::on_tableWidget_itemChanged);
}

DictionaryDialog::~DictionaryDialog()
{
    delete ui;
    delete settings;
}

QList<DictEntry> DictionaryDialog::getDictionary() const
{
    QList<DictEntry> dictionary;

    // 遍历表格，获取字典内容
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        DictEntry entry;
        entry.index = ui->tableWidget->item(i, 0)->text();
        entry.original = ui->tableWidget->item(i, 1)->text();
        entry.meaning = ui->tableWidget->item(i, 2)->text();
        entry.enabled = (ui->tableWidget->item(i, 3)->checkState() == Qt::Checked);
        dictionary.append(entry);
    }

    return dictionary;
}

void DictionaryDialog::on_pb_add_clicked()
{
    // 获取当前行数
    int row = ui->tableWidget->rowCount();

    // 插入新行
    ui->tableWidget->insertRow(row);

    // 设置序号
    QTableWidgetItem *itemIndex = new QTableWidgetItem(QString::number(row + 1));
    ui->tableWidget->setItem(row, 0, itemIndex);

    // 设置默认的原始值、释义和是否启用
    ui->tableWidget->setItem(row, 1, new QTableWidgetItem("New Word"));
    ui->tableWidget->setItem(row, 2, new QTableWidgetItem("New Meaning"));
    QTableWidgetItem *itemEnabled = new QTableWidgetItem("true");
    itemEnabled->setCheckState(Qt::Checked);
    ui->tableWidget->setItem(row, 3, itemEnabled);
}

void DictionaryDialog::on_pb_del_clicked()
{
    // 获取当前选中的行
    int currentRow = ui->tableWidget->currentRow();

    // 如果选中了有效行，则删除
    if (currentRow >= 0) {
        ui->tableWidget->removeRow(currentRow);
    }

    // 更新序号
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->item(i, 0)->setText(QString::number(i + 1));
    }

    // 标记为已修改
    modified = true;
}

void DictionaryDialog::on_tableWidget_itemChanged(QTableWidgetItem *item)
{
    // 当表格内容被修改时，标记为已修改
    if (item) {
        modified = true;
        ui->tableWidget->resizeColumnsToContents();
    }
}

void DictionaryDialog::saveDictionary()
{
    // 清空之前的配置
    settings->remove("dictionary");

    // 开始写入数组
    settings->beginWriteArray("dictionary");
    // 保存词典配置
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        settings->setArrayIndex(i); // 设置当前数组索引
        settings->setValue("index", ui->tableWidget->item(i, 0)->text());
        settings->setValue("original", ui->tableWidget->item(i, 1)->text());
        settings->setValue("meaning", ui->tableWidget->item(i, 2)->text());
        settings->setValue("enabled", ui->tableWidget->item(i, 3)->checkState() == Qt::Checked);
    }
    settings->endArray(); // 结束写入数组

    // 保存完成后，重置 modified 标记
    modified = false;
}

void DictionaryDialog::loadDictionary()
{
    // 清空表格
    ui->tableWidget->setRowCount(0);

    // 加载词典配置
    int size = settings->beginReadArray("dictionary"); // 开始读取数组
    for (int i = 0; i < size; ++i) {
        settings->setArrayIndex(i); // 设置当前数组索引

        // 插入新行
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);

        // 设置序号
        QTableWidgetItem *itemIndex = new QTableWidgetItem(settings->value("index").toString());
        ui->tableWidget->setItem(row, 0, itemIndex);

        // 设置原始值
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(settings->value("original").toString()));

        // 设置释义
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(settings->value("meaning").toString()));

        // 设置是否启用
        QTableWidgetItem *itemEnabled = new QTableWidgetItem();
        itemEnabled->setCheckState(settings->value("enabled").toBool() ? Qt::Checked : Qt::Unchecked);
        ui->tableWidget->setItem(row, 3, itemEnabled);
    }
    settings->endArray(); // 结束读取数组
    ui->tableWidget->resizeColumnsToContents();

    // 加载完成后，重置 modified 标记
    modified = false;
}

void DictionaryDialog::on_pb_editor_clicked()
{
//    TextDialog dialog;
    TextKVDialog dialog;
    if (dialog.exec() == QDialog::Accepted) {
        auto kv = dialog.keyMap();
        foreach (auto key, kv.keys()) {
            // 插入新行
            int row = ui->tableWidget->rowCount();
            ui->tableWidget->insertRow(row);

            // 设置序号
            QTableWidgetItem *itemIndex = new QTableWidgetItem(QString::number(row));
            ui->tableWidget->setItem(row, 0, itemIndex);

            // 设置原始值
            ui->tableWidget->setItem(row, 1, new QTableWidgetItem(key));

            // 设置释义
            ui->tableWidget->setItem(row, 2, new QTableWidgetItem(kv.value(key)));

            // 设置是否启用
            QTableWidgetItem *itemEnabled = new QTableWidgetItem();
            itemEnabled->setCheckState(Qt::Checked);
            ui->tableWidget->setItem(row, 3, itemEnabled);
        }
    }
}

